package cn.com.xy.sms.sdk.number;

import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import cn.com.xy.sms.sdk.constant.Constant;
import cn.com.xy.sms.sdk.log.LogManager;
import cn.com.xy.sms.sdk.util.BaseRunnable;
import cn.com.xy.sms.sdk.util.DuoquUtils;
import cn.com.xy.sms.sdk.util.XyUtil;

/* SDK-428 huangzhiqiang 20160615 start */
public class LocationRunnable extends BaseRunnable {
    private static BaseRunnable mBaseRunnable = null;
	private static HandlerThread mHandlerThread = null;
    private static Handler mHandler = null;
    private static boolean mFirstLocation = true;

	public static BaseRunnable getInstance() {
		synchronized (LocationRunnable.class) {
			if (mBaseRunnable == null) {
				mBaseRunnable = new LocationRunnable();							
			}
		}
		
		return mBaseRunnable;
	}
	
	private LocationRunnable() {
		mExecuteInterval = Constant.MINUTE * 20;
	}
	
    @Override
    public void execute() {
        try {
            if (mHandler == null) {
                initHandler();
            }

            DuoquUtils.getSdkDoAction().getLocation(Constant.getContext(), mHandler);
        } catch (Throwable e) {
            LogManager.e(Constant.TAG, "LocationRunnable execute error:", e);
        }
    }

    @Override
    public void setThreadPriority() {
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
        android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
    }
    
    private void initHandler() {
        mHandlerThread = new HandlerThread("LocationHandlerThread");
        mHandlerThread.start();
        mHandler = new Handler(mHandlerThread.getLooper()) {

            @Override
            public void handleMessage(Message msg) {
                try {
                    Bundle bundle = msg.getData();
                    if (paramsError(bundle)) {
                        return;
                    }

                    double latitude = bundle.getDouble("latitude");
                    double longitude = bundle.getDouble("longitude");
                    if (latitude == 0 && longitude == 0) {
                        // 第一次定位不成功时5秒后再次定位
                        if (mFirstLocation) {
                            mFirstLocation = false;
                            Thread.sleep(5000);
                            DuoquUtils.getSdkDoAction().getLocation(Constant.getContext(), mHandler);
                        }
                        return;
                    }

                    XyUtil.setLoactionInfo(latitude, longitude);
                    // 清空旧区号，经纬度变化以后需要重新获取区号
                    XyUtil.removeAreaCodeInfo();
                } catch (Throwable e) {
                    LogManager.e(Constant.TAG, "LocationRunnable initHandler error:", e);
                }
            }

            private boolean paramsError(Bundle bundle) {
                return bundle == null || !bundle.containsKey("latitude") || !bundle.containsKey("longitude");
            }
        };
    }
}
/* SDK-428 huangzhiqiang 20160615 end */